package com.example.demo.pojo.exception;

import com.example.demo.pojo.enums.common.BusinessErrorEnum;
import com.example.demo.pojo.enums.common.SysErrorEnum;
import lombok.Data;

/**
 * @author tds
 * @version 1.0.0
 * @Company
 * @ClassName BusinessException
 * @Description 自定义的业务异常
 * @Date 2022-03-28 23:44
 */
@Data
public class BusinessException extends RuntimeException{

    /**
     * 错误码
     */
    private String code;

    /**
     * 描述信息
     */
    private String msg;

    public BusinessException(){
        this.code = SysErrorEnum.SYS_ERROR.getCode();
        this.msg = SysErrorEnum.SYS_ERROR.getName();
    }

    public BusinessException(String code){
        // 获取code码对应的错误信息，没有默认SYS_ERROR
        BusinessErrorEnum businessErrorEnum = BusinessErrorEnum.valueCodeOf(code);
        this.code = code;
        this.msg = businessErrorEnum == null ? SysErrorEnum.SYS_ERROR.getName() : businessErrorEnum.getName();
    }

    public BusinessException(BusinessErrorEnum businessErrorEnum){
        this.code = businessErrorEnum.getCode();
        this.msg = businessErrorEnum.getName();
    }
}
